package com.easyjava.qingke.project.command.impl;

import com.easyjava.qingke.project.command.SystemCommand;
import com.easyjava.qingke.project.mybatis.pojo.CollarSingle;
import com.easyjava.qingke.project.mybatis.pojo.CollarSingleML;
import com.easyjava.qingke.project.mybatis.pojo.LibraryRecord;
import com.easyjava.qingke.project.mybatis.pojo.LibraryRecordML;
import com.easyjava.qingke.project.mybatis.pojo.PurchaseOrder;
import com.easyjava.qingke.project.mybatis.pojo.PurchaseOrderML;
import com.easyjava.qingke.project.mybatis.pojo.PurchasePlanML;
import com.easyjava.qingke.project.mybatis.pojo.PurchasePlanning;
import com.easyjava.qingke.project.mybatis.pojo.StorageML;
import com.easyjava.qingke.project.mybatis.pojo.StorageRecord;
import com.easyjava.qingke.project.util.ProjectConsole;

public class ProduceCommand extends SystemCommand{

	public void excute() {
		
		ProjectConsole.println("1.生成采购单");
		ProjectConsole.println("2.生成入库记录");
		ProjectConsole.println("3.生成出库记录");
		
		int id=ProjectConsole.promptInt("请输入想要执行的操作的数字:");
		while(id>0){
		if(1==id){
			producePurchaseOrder();
			break;
		}else if (2==id){
			produceStorageRecord();
			break;
		}else if(3==id){
			produceLibraryRecord();
			break;
		}else
			ProjectConsole.println("输入的数字不存在！");
		    id=ProjectConsole.promptInt("请重新输入想要执行的操作的数字:");
	}
	}

	private void produceLibraryRecord() {
		dao.selectCollarSingle();
		ProjectConsole.println();
		int id =ProjectConsole.promptInt("请输入要出库的领用单的ID：");
		while(!dao.isCollarSingleIdExists(id)){
			ProjectConsole.println("该ID不存在！");
			id =ProjectConsole.promptInt("请重新输入要出库的领用单的ID：");
		}
		CollarSingle collarSingle=dao.getCollarSingleById(id);
		CollarSingleML collarSingleML=dao.getByCollarSingleId(id);
		
		LibraryRecord libraryRecord=new LibraryRecord();
		String code =ProjectConsole.prompt("请输入编码：");
		while(dao.isLibraryRecordCodeExists(code)){
			ProjectConsole.println("该编码已被使用！");
			code=ProjectConsole.prompt("请重新输入编码：");
		}
		String description=ProjectConsole.prompt("请描述：");
		String name=ProjectConsole.prompt("请输入修改姓名：");
		libraryRecord.setCode(code);
		libraryRecord.setDescription(description);
		libraryRecord.setProjectId(collarSingle.getProjectId());
		libraryRecord.setCollarSingleId(id);
		libraryRecord.setLastModifiedPerson(name);
		
		dao.produceLibraryRecord(libraryRecord);
		
		CollarSingle cs =new CollarSingle();
		cs.setId(id);
		cs.setStatus("已出库");
		dao.changeCollarSingle(cs);
		
		
		ProjectConsole.println("已生成出库记录，自行创建出库材料清单！");
		
		LibraryRecordML libraryRecordML=new LibraryRecordML();
		libraryRecordML.setLibraryRecordId(dao.getLibraryRecordIdByCode(code));
		libraryRecordML.setMaterialId(collarSingleML.getMaterialId());
		libraryRecordML.setAmount(collarSingleML.getAmount());
		
		dao.produceLibraryRecordML(libraryRecordML);
		
		
	}

	private void produceStorageRecord() {
		dao.selectPurchaseOrder();
		ProjectConsole.println();
		int id =ProjectConsole.promptInt("请输入要入库的采购单ID:");
		while(!dao.isPurchaseOrderIdExists(id)){
			ProjectConsole.println("该ID不存在！");
			id=	ProjectConsole.promptInt("请重新输入要入库的采购单ID:");	
		}
		PurchaseOrder purchaseOrder=dao.getPurchaseOrderById(id);
		PurchaseOrderML purchaseOrderML=dao.getByPurchaseOrderId(id);
		
		StorageRecord storageRecord=new StorageRecord();
		String code =ProjectConsole.prompt("请输入编码：");
		while(dao.isStorageRecordCodeExists(code)){
			ProjectConsole.println("该编码已被使用！");
			code=ProjectConsole.prompt("请重新输入编码：");
		}
		String description=ProjectConsole.prompt("请描述：");
		String name=ProjectConsole.prompt("请输入修改姓名：");
		storageRecord.setCode(code);
		storageRecord.setDescription(description);
		storageRecord.setProjectId(purchaseOrder.getProjectId());
		storageRecord.setPurchaseOrderId(id);
		storageRecord.setLastModifiedPerson(name);
		
		dao.produceStorageRecord(storageRecord);
		
		PurchaseOrder po=new PurchaseOrder();
		po.setId(id);
		po.setStatus("已入库");
		dao.changePurchaseOrder(po);
		
		ProjectConsole.println("已生成入库记录，自行创建入库材料清单！");
		
		StorageML storageML=new StorageML();
		storageML.setStorageRecordId(dao.getStorageRecordIdByCode(code));
		storageML.setMaterialId(purchaseOrderML.getMaterialId());
		storageML.setAmount(purchaseOrderML.getAmount());
		
		dao.produceStorageML(storageML);
		
	}

	private void producePurchaseOrder() {
        dao.selectPurchasePlanning();
        ProjectConsole.println();
		int id = ProjectConsole.promptInt("请输入要生成采购单的采购计划ID:");
		while(!dao.isPurchasePlanningIdExists(id)){
			ProjectConsole.println("该ID不存在！");
			id=	ProjectConsole.promptInt("请重新输入要生成采购单的采购计划ID:");	
		}
		PurchasePlanning purchasePlanning=dao.getPurchasePlanningById(id);
		
		PurchaseOrder purchaseOrder =new PurchaseOrder();
		String code =ProjectConsole.prompt("请输入编码：");
		while(dao.isPurchaseOrderCodeExists(code)){
			ProjectConsole.println("该编码已被使用！");
			code=ProjectConsole.prompt("请重新输入编码：");
		}
		String description=ProjectConsole.prompt("请描述：");
		String name=ProjectConsole.prompt("请输入修改人姓名：");
		purchaseOrder.setCode(code);
		purchaseOrder.setDescription(description);
		purchaseOrder.setProjectId(purchasePlanning.getProjectId());
		purchaseOrder.setPurchasePlanningId(id);
		purchaseOrder.setStatus("未入库");
		purchaseOrder.setLastModifiedPerson(name);
		
		dao.producePurchaseOrder(purchaseOrder);
		
		ProjectConsole.println("已生成采购单，需创建采购单材料清单！");
		PurchasePlanML purchasePlanML=dao.getByPurchasePlanningId(id);
		
		PurchaseOrderML purchaseOrderML=new PurchaseOrderML();
		dao.selectMaterial();
		ProjectConsole.println();
		int materialId =ProjectConsole.promptInt("请输入材料ID：");
		int amount =ProjectConsole.promptInt("请输入数量：");
		while(amount>purchasePlanML.getAmount()){
			ProjectConsole.println("该数量超过了采购计划材料的数量！");
			amount=ProjectConsole.promptInt("请重新输入数量：");
		}
		purchaseOrderML.setPurchaseOrderId(dao.getPurchaseOrderId(code));
		purchaseOrderML.setMaterialId(materialId);
		purchaseOrderML.setAmount(amount);
		
		dao.producePurchaseOrderML(purchaseOrderML);
	}

}
